import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'datas.dart';

bool Win(List<Offset> offs, Offset offset) {
  var qx = 6;
  var qy = 5;

  //左
  List<Offset> l = [];
  int lLenght = 0;
  for (var x = offset.dx; x >= 0; x--) {
    var item = Offset(x, offset.dy);
    if (offs.contains(item)) {
      lLenght++;
      l.add(item);
      if (lLenght >= 4) {
        return true;
      }
    } else {
      break;
    }
  }

  // 右
  List<Offset> r = [];
  int rLenght = 0;
  for (var x = offset.dx; x <= qx; x++) {
    var item = Offset(x, offset.dy);
    if (offs.contains(item)) {
      rLenght++;
      r.add(item);
      if (rLenght >= 4) {
        return true;
      }
    } else {
      break;
    }
  }

  // 下
  List<Offset> b = [];
  int bLenght = 0;
  for (var y = offset.dy; y >= 0; y--) {
    var item = Offset(offset.dx, y);
    if (offs.contains(item)) {
      bLenght++;
      b.add(item);
      if (bLenght >= 4) {
        return true;
      }
    } else {
      break;
    }
  }

  // 左上
  List<Offset> lt = [];
  int ltLenght = 0;
  for (var y = offset.dy, x = offset.dx;
      x >= 0 && y <= qy;
      y = y + 1, x = x - 1) {
    var item = Offset(x, y);
    if (offs.contains(item)) {
      ltLenght++;
      lt.add(item);
      if (ltLenght >= 4) {
        return true;
      }
    } else {
      break;
    }
  }

  // 右上
  List<Offset> rt = [];
  int rtLenght = 0;
  for (var y = offset.dy, x = offset.dx;
      x <= qx && y <= qy;
      y = y + 1, x = x + 1) {
    var item = Offset(x, y);
    if (offs.contains(item)) {
      rtLenght++;
      rt.add(item);
      if (rtLenght >= 4) {
        return true;
      }
    } else {
      break;
    }
  }

  // 右下
  List<Offset> rb = [];
  int rbLenght = 0;
  for (var y = offset.dy, x = offset.dx;
      x <= qx && y >= 0;
      y = y - 1, x = x + 1) {
    var item = Offset(x, y);
    if (offs.contains(item)) {
      rbLenght++;
      rb.add(item);
      if (rbLenght >= 4) {
        return true;
      }
    } else {
      break;
    }
  }

  // 左下
  List<Offset> lb = [];
  int lbLenght = 0;
  for (var y = offset.dy, x = offset.dx;
      x >= 0 && y >= 0;
      y = y - 1, x = x - 1) {
    var item = Offset(x, y);
    if (offs.contains(item)) {
      lbLenght++;
      lb.add(item);
      if (lbLenght >= 4) {
        return true;
      }
    } else {
      break;
    }
  }

  if (l.length + r.length == 5) {
    return true;
  }

  if (lt.length + rb.length == 5) {
    return true;
  }

  if (rt.length + lb.length == 5) {
    return true;
  }

  return false;
}
